<!--
  * 工单BOM详情
  *
  * @Author:    lmabbe
  * @Date:      2025-07-03 17:39:42
  * @Copyright  lmabbe
-->
<template>
  <a-drawer :destroyOnClose="true" :maskClosable="false" :open="visibleFlag" :title="form.bomId ? '编辑' : '添加'" :width="450" @close="onClose">
    <a-form ref="formRef" :label-col="{ span: 5 }" :model="form" :rules="rules">
      <a-row>
        <a-col :span="12">
          <a-form-item label="工单BOM编号" name="bomId">
            <a-input-number v-model:value="form.bomId" placeholder="工单BOM编号" style="width: 100%" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="工单ID" name="workOrderId">
            <a-input-number v-model:value="form.workOrderId" placeholder="工单ID" style="width: 100%" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="物料类型" name="materialType">
            <SmartEnumSelect
              v-model:value="form.materialType"
              enum-name="WORK_ORDER_BOM_DETAIL_MATERIAL_TYPE_ENUM"
              placeholder="物料类型"
              width="100%"
            />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="物料编码" name="materialCode">
            <a-input v-model:value="form.materialCode" placeholder="物料编码" style="width: 100%" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="主料物料编码" name="mainMaterialCode">
            <a-input v-model:value="form.mainMaterialCode" placeholder="主料物料编码" style="width: 100%" />
          </a-form-item>
        </a-col>
        <a-col :span="12"></a-col>
      </a-row>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-drawer>
</template>
<script setup>
  import { nextTick, reactive, ref } from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';

  import { workOrderBomDetailApi } from '/@/api/business/product/order/work-order-bom-detail-api.js';
  import { smartSentry } from '/@/lib/smart-sentry';
  import SmartEnumSelect from '/@/components/framework/smart-enum-select/index.vue';

  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList']);

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  function show(rowData) {
    Object.assign(form, formDefault);
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
    }
    // 使用字典时把下面这注释修改成自己的字典字段 有多个字典字段就复制多份同理修改 不然打开表单时不显示字典初始值
    // if (form.status && form.status.length > 0) {
    //   form.status = form.status.map((e) => e.valueCode);
    // }
    visibleFlag.value = true;
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }

  function onClose() {
    Object.assign(form, formDefault);
    visibleFlag.value = false;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
    bomId: undefined, //工单BOM编号
    workOrderId: undefined, //工单ID
    materialType: undefined, //物料类型:[1:主料，2:替代料]
    materialCode: undefined, //物料编码
    mainMaterialCode: undefined, //主料物料编码
    dosage: undefined, //用量
  };

  let form = reactive({ ...formDefault });

  const rules = {
    bomId: [{ required: true, message: '工单BOM编号 必填' }],
    workOrderId: [{ required: true, message: '工单ID 必填' }],
    materialType: [{ required: true, message: '物料类型:[1:主料，2:替代料] 必填' }],
    materialCode: [{ required: true, message: '物料编码 必填' }],
    mainMaterialCode: [{ required: true, message: '主料物料编码 必填' }],
    dosage: [{ required: true, message: '用量 必填' }],
  };

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      save();
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show();
    try {
      if (form.bomId) {
        await workOrderBomDetailApi.update(form);
      } else {
        await workOrderBomDetailApi.add(form);
      }
      message.success('操作成功');
      emits('reloadList');
      onClose();
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  defineExpose({
    show,
  });
</script>
